package com.prism.moodme.app;

import java.awt.GridBagLayout;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JComboBox;

import com.prism.moodme.util.Utility;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import java.awt.FlowLayout;
import javax.swing.BoxLayout;

public class SearchPanel extends JPanel {

	private JComboBox monthComboBox = null;
	private JComboBox dayComboBox = null;
	private JComboBox hourComboBox = null;
	private JButton searchButton = null;
	private JComboBox minComboBox = null;
	private MainFrame mainFrame = null;  //  @jve:decl-index=0:visual-constraint="0,1"
	private SearchActionListener searchActionListener;  //  @jve:decl-index=0:

	public int month = 3;
	public int day = 1;
	public int hour = 0;
	public int min = 0;
	private JPanel TopPanel = null;
	private JPanel BottomPanel = null;
	/**
	 * This is the default constructor
	 */
	public SearchPanel(MainFrame mainFrame) {
		super();
		setMainFrame(mainFrame);
		this.setSearchActionListener(new SearchActionListener(getMainFrame()));
		initialize();
	}

	/**
	 * This method initializes this
	 *
	 * @return void
	 */
	private void initialize() {
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.setSize(new Dimension(400, 150));
		this.setPreferredSize(new Dimension(400, 150));
		this.add(getTopPanel(), null);
		this.add(getBottomPanel(), null);

	}

	/**
	 * This method initializes monthComboBox
	 *
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getMonthComboBox() {
		if (monthComboBox == null) {
			monthComboBox = new JComboBox(Utility._MONTHS);
			monthComboBox.setPreferredSize(new Dimension(70, 25));
			monthComboBox.setSelectedIndex(4);
			monthComboBox.setEnabled(false);
			monthComboBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					String sel = (String)getMonthComboBox().getSelectedItem();
					if(!sel.equalsIgnoreCase("MONTH"))
						month = Utility.getMonth(sel);
				}
			});
			monthComboBox.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println(e.getActionCommand());
				}
			});
		}
		return monthComboBox;
	}

	/**
	 * This method initializes dayComboBox
	 *
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getDayComboBox() {
		if (dayComboBox == null) {
			dayComboBox = new JComboBox(Utility._DAYS);
			dayComboBox.setPreferredSize(new Dimension(70, 25));
			dayComboBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					String sel = (String)getDayComboBox().getSelectedItem();
					if(!sel.equalsIgnoreCase("DAY"))
						day = Utility.getDay(sel);
				}
			});
		}
		return dayComboBox;
	}

	/**
	 * This method initializes hourComboBox
	 *
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getHourComboBox() {
		if (hourComboBox == null) {
			hourComboBox = new JComboBox(Utility._HOURS);
			hourComboBox.setSelectedIndex(0);
			hourComboBox.setPreferredSize(new Dimension(70, 25));
			hourComboBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					String sel = (String)getHourComboBox().getSelectedItem();
					if(!sel.equalsIgnoreCase("HOUR"))
						hour = Utility.getHour(sel);
				}
			});
		}
		return hourComboBox;
	}

	/**
	 * This method initializes searchButton
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getSearchButton() {
		if (searchButton == null) {
			searchButton = new JButton("SEARCH");
			searchButton.addActionListener(getSearchActionListener());
			searchButton.setAction(new SearchAction(this));


		}
		return searchButton;
	}

	/**
	 * This method initializes HourComboBox
	 *
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getMinComboBox() {
		if (minComboBox == null) {
			minComboBox = new JComboBox(Utility._MINUTES);
			minComboBox.setPreferredSize(new Dimension(70, 25));
			minComboBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					String sel = (String)getMinComboBox().getSelectedItem();
					if(!sel.equalsIgnoreCase("MINUTE")){
						System.out.println(sel);
						min = Utility.getMin(sel);
					}
				}
			});
		}
		return minComboBox;
	}

	/**
	 * This method initializes TopPanel
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getTopPanel() {
		if (TopPanel == null) {
			TopPanel = new JPanel();
			TopPanel.setLayout(new FlowLayout());
			TopPanel.setPreferredSize(new Dimension(500, 40));
			TopPanel.add(getMonthComboBox(), null);
			TopPanel.add(getDayComboBox(), null);
			TopPanel.add(getHourComboBox(), null);
			TopPanel.add(getMinComboBox(), null);
		}
		return TopPanel;
	}

	/**
	 * This method initializes BottomPanel
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getBottomPanel() {
		if (BottomPanel == null) {
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 0;
			BottomPanel = new JPanel();
			BottomPanel.setLayout(new GridBagLayout());
			BottomPanel.setPreferredSize(new Dimension(500, 460));
			BottomPanel.add(getSearchButton(), gridBagConstraints);
		}
		return BottomPanel;
	}

	public void setMainFrame(MainFrame main) {
		this.mainFrame = main;
	}

	public MainFrame getMainFrame() {

		return this.mainFrame;
	}

	public void setSearchActionListener(SearchActionListener searchActionListener) {
		this.searchActionListener = searchActionListener;
	}

	public SearchActionListener getSearchActionListener() {
		return searchActionListener;
	}

}  //  @jve:decl-index=0:visual-constraint="164,37"
